using System;
using System.ComponentModel;
using System.Windows.Controls;
using Creventive.Wpf.ShellFactory.Workspace.Primitives;

namespace Creventive.Wpf.ShellFactory.Workspace
{
    /// <summary>
    ///   Represents a single item in a workspace tab strip.
    /// </summary>
    public class WorkspaceTabItem : TabItem
    {
        private bool isInternalSelectionStatusChange;
        private DependencyPropertyDescriptor isActivePropertyDescriptor;

        /// <summary>
        ///   Initializes a new instance of the <see cref = "WorkspaceTabItem" /> class.
        /// </summary>
        public WorkspaceTabItem()
        {
            DependencyPropertyDescriptor isSelectedDescriptor = DependencyPropertyDescriptor.FromProperty(IsSelectedProperty, this.GetType());
            isSelectedDescriptor.AddValueChanged(this, this.this_IsSelectedChanged);
        }

        /// <summary>
        ///   Called when the header content has changed.
        /// </summary>
        /// <param name = "oldContent">The old content.</param>
        /// <param name = "newContent">The new content.</param>
        protected override sealed void OnHeaderChanged(object oldContent, object newContent)
        {
            var oldValue = (WorkspaceItem)oldContent;
            var newValue = (WorkspaceItem)newContent;
            if (oldValue != newValue)
                this.OnWorkspaceItemChanged(oldValue, newValue);
            // Override all base behaviors as the workspace items already have logical parents, we just want to present information about them
        }

        /// <summary>
        ///   Called when the IsSelected property has changed.
        /// </summary>
        protected virtual void OnIsSelectedChanged()
        {
            if (!this.isInternalSelectionStatusChange)
            {
                if (this.IsSelected)
                {
                    WorkspaceItem item = (WorkspaceItem)this.Header;
                    item.Workspace.Activate(item.ApplicationView);
                }
            }
        }

        /// <summary>
        ///   Called when the workspace item for this tab has changed.
        /// </summary>
        /// <param name = "oldValue">The old value.</param>
        /// <param name = "newValue">The new value.</param>
        protected virtual void OnWorkspaceItemChanged(WorkspaceItem oldValue, WorkspaceItem newValue)
        {
            this.isActivePropertyDescriptor = DependencyPropertyDescriptor.FromProperty(WorkspaceItem.IsActiveProperty, newValue.GetType());
            this.isActivePropertyDescriptor.AddValueChanged(this, this.WorkspaceItem_IsActiveChanged);
        }

        private void WorkspaceItem_IsActiveChanged(object sender, EventArgs e)
        {
            this.isInternalSelectionStatusChange = true;
            try
            {
                this.IsSelected = ((WorkspaceItem)sender).IsActive;
            }
            finally
            {
                this.isInternalSelectionStatusChange = false;
            }
        }

        private void this_IsSelectedChanged(object sender, EventArgs e)
        {
            this.OnIsSelectedChanged();
        }
    }
}